﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SuperMarketSystem
{
    public partial class SelfCheckControl : UserControl
    {
        event CheckoutEventHandler checkoutEvent;
        event ItemLookupEventHandler lookupEvent;
        event TransactionEventHandler transactionEvent;
        event FinishedEventHandler finishEvent;

        public SelfCheckControl(SelfCheckStation parent)
        {
            InitializeComponent();
            checkoutEvent += parent.checkouthandler;
            lookupEvent += parent.lookuphandler;
            transactionEvent += parent.transactionhandler;
            finishEvent += parent.finishhandler;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (checkoutEvent != null)
            {
                if (checkoutEvent(this))
                {
                    List<Purchase> transactions = new List<Purchase>();
                    transactionEvent(this, transactions);
                    finishEvent(this);
                }
                else
                {
                    if (MessageBox.Show("Payment Invalid. Retry?",
                        "Payment Error", MessageBoxButtons.OKCancel)
                        == DialogResult.Cancel)
                        finishEvent(this);
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (finishEvent != null)
                finishEvent(this);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (lookupEvent != null)
                    lookupEvent(this, int.Parse(textBox2.Text));
                textBox2.Text = "";
            }
            catch (Exception error)
            {
                MessageBox.Show(error.ToString());
            }
        }
    }
}
